//
//  UserSettingViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/5.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "UserSettingViewController.h"

#import "UserSettingTableViewCell.h"
#import "WodejifenViewController.h"
#import "WodeqianbaoViewController.h"
#import "XuanzeshouhuoViewController.h"
#import "WodeshoucangViewController.h"
#import "YijianfankuiViewController.h"
#import "ShezhiViewController.h"
#import "DingdanzhangtaiViewController.h"
#import "LoginViewController.h"

@interface UserSettingViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

//头视图
@property (nonatomic, strong) UIView * view_headView;
//
@property (nonatomic, strong) UIImageView * image_touxiang;
@property (nonatomic, strong) UIButton * btn_login;
//
@property (nonatomic, strong) UIButton * user_name;
//
@property (nonatomic, strong) UILabel * jifen;
@property (nonatomic, strong) UILabel * qianbao;

@end

@implementation UserSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _lblTitle.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.hidden = YES;
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] showTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //头视图
    [self p_headView];
    self.tableView.tableHeaderView = self.view_headView;
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[UserSettingTableViewCell class] forCellReuseIdentifier:@"cell_user"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        return 70;
    }
    else
    {
        return 50;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    if(indexPath.row == 0)
    {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line];
        
        UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 25, 20, 20)];
        image.image = [UIImage imageNamed:@"dingwei@2x.png"];
        [cell addSubview:image];
        
        UILabel * name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image.frame) + 10, 25, 150, 20)];
        name.text = @"管理收货地址";
        name.font = [UIFont systemFontOfSize:15];
        [cell addSubview:name];
        
        UIImageView * image_icon = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 15 - 10, 27.5, 10, 15)];
        image_icon.image = [UIImage imageNamed:@"youfanhui@2x.png"];
        [cell addSubview:image_icon];
        
        UIView * view_line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 60, SCREEN_WIDTH, 10)];
        view_line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell addSubview:view_line1];
    }
    else
    {
        UserSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_user" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        switch (indexPath.row)
        {
            case 1:
            {
                cell.name.text = @"我的收藏";
                cell.image.image = [UIImage imageNamed:@"shoucan_1@2x.png"];
            }
                break;
            case 2:
            {
                cell.name.text = @"客服中心";
                cell.image.image = [UIImage imageNamed:@"kefu@2x.png"];
            }
                break;
            case 3:
            {
                cell.name.text = @"意见反馈";
                cell.image.image = [UIImage imageNamed:@"yijianfankuai@2x.png"];
            }
                break;
            case 4:
            {
                cell.name.text = @"设置";
                cell.image.image = [UIImage imageNamed:@"setting@2x.png"];
            }
                break;
            default:
                break;
        }
        return cell;
    }
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row)
    {
        case 0:
        {
            XuanzeshouhuoViewController * xuanzeshouhuoViewController = [[XuanzeshouhuoViewController alloc] init];
            [self showViewController:xuanzeshouhuoViewController sender:nil];
        }
            break;
        case 1:
        {
            WodeshoucangViewController * wodeshoucangViewController = [[WodeshoucangViewController alloc] init];
            [self showViewController:wodeshoucangViewController sender:nil];
        }
            break;
        case 2:
        {
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"24小时客服电话" message:@"400-8888-888" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            
            UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:action];
            
            UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"拨打" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                NSURL * url_tel = [NSURL URLWithString:[NSString stringWithFormat:@"tel://400-8888-888"]];
                [[UIApplication sharedApplication] openURL:url_tel];
                
            }];
            [alert addAction:action1];
        }
            break;
        case 3:
        {
            YijianfankuiViewController * yijianfankuiViewController = [[YijianfankuiViewController alloc] init];
            [self showViewController:yijianfankuiViewController sender:nil];
        }
            break;
        case 4:
        {
            ShezhiViewController * shezhiViewController = [[ShezhiViewController alloc] init];
            [self showViewController:shezhiViewController sender:nil];
            
        }
            break;
        default:
            break;
    }
}

#pragma mark - 未登录点击后跳到登录页面
- (void )btn_loginAction:(UIButton *)sender
{
//    NSLog(@"跳到登录页面");
    
    LoginViewController * loginViewController = [[LoginViewController alloc] init];
    [self showViewController:loginViewController sender:nil];
}

#pragma mark - 登录状态下跳到修改头像页面
- (void )btn_xiugaitouxiangAction:(UIButton *)sender
{
    NSLog(@"跳到修改头像页面");
}

#pragma mark - 积分
- (void )btn_jifenAction:(UIButton *)sender
{
//    NSLog(@"判断是否处于登录状态,未登录跳登录,登录跳积分");
    
    WodejifenViewController * wodejifenViewController = [[WodejifenViewController alloc] init];
    [self showViewController:wodejifenViewController sender:nil];
}

#pragma mark - 钱包
- (void )btn_qianbaoAction:(UIButton *)sender
{
//    NSLog(@"判断是否处于登录状态,未登录跳登录,登录跳钱包");
    
    WodeqianbaoViewController * wodeqianbaoViewController = [[WodeqianbaoViewController alloc] init];
    [self showViewController:wodeqianbaoViewController sender:nil];
}

#pragma mark - 购买的商品所处状态
- (void )btn_typeAction:(UIButton *)sender
{
    DingdanzhangtaiViewController * dingdanzhangtaiViewController = [[DingdanzhangtaiViewController alloc] init];
    [self showViewController:dingdanzhangtaiViewController sender:nil];
    
    switch (sender.tag - 10)
    {
        case 0:
        {
//            NSLog(@"跳到未支付");
            dingdanzhangtaiViewController.dingdan_type = 0;
        }
            break;
        case 1:
        {
//            NSLog(@"跳到待接单");
            dingdanzhangtaiViewController.dingdan_type = 1;
        }
            break;
        case 2:
        {
//            NSLog(@"跳到配送中");
            dingdanzhangtaiViewController.dingdan_type = 2;
        }
            break;
        case 3:
        {
//            NSLog(@"跳到已完成");
            dingdanzhangtaiViewController.dingdan_type = 3;
        }
            break;
        case 4:
        {
//            NSLog(@"跳到已取消");
            dingdanzhangtaiViewController.dingdan_type = 4;
        }
            break;
        default:
            break;
    }
}

#pragma mark - 头视图
- (void )p_headView
{
    self.view_headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 330)];
    self.view_headView.backgroundColor = [UIColor whiteColor];
    
    //
    UIImageView * image_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    image_bg.image = [UIImage imageNamed:@"user_bg@2x.png"];
    [self.view_headView addSubview:image_bg];
    
    //未登录
    self.image_touxiang = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, 60, 80, 80)];
    self.image_touxiang.layer.cornerRadius = 40;
    self.image_touxiang.image = [UIImage imageNamed:@"user_touxiangW@2x.png"];
    self.image_touxiang.userInteractionEnabled = YES;
    [self.view_headView addSubview:self.image_touxiang];
    
    UIButton * btn_Wlogin = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Wlogin.frame = CGRectMake(0, 0, 80, 80);
    [self.image_touxiang addSubview:btn_Wlogin];
    [btn_Wlogin addTarget:self action:@selector(btn_loginAction:) forControlEvents:(UIControlEventTouchUpInside)];

    self.btn_login = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_login.frame = CGRectMake(SCREEN_WIDTH / 2 - 40, CGRectGetMaxY(self.image_touxiang.frame) + 15, 80, 30);
    self.btn_login.layer.cornerRadius = 5;
    self.btn_login.layer.borderColor = navi_bar_bg_color.CGColor;
    self.btn_login.layer.borderWidth = 1;
    [self.btn_login setTitle:@"注册/登录" forState:(UIControlStateNormal)];
    [self.btn_login setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
    [self.view_headView addSubview:self.btn_login];
    [self.btn_login addTarget:self action:@selector(btn_loginAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
//    //登录
//    self.image_touxiang = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 40, 60, 80, 80)];
//    self.image_touxiang.layer.cornerRadius = 40;
//    self.image_touxiang.userInteractionEnabled = YES;
//    self.image_touxiang.image = [UIImage imageNamed:@"user_touxiangD.png"];
//    [self.view_headView addSubview:self.image_touxiang];
//    
//    UIButton * btn_Wlogin = [UIButton buttonWithType:(UIButtonTypeSystem)];
//    btn_Wlogin.frame = CGRectMake(0, 0, 80, 80);
//    [self.image_touxiang addSubview:btn_Wlogin];
//    [btn_Wlogin addTarget:self action:@selector(btn_xiugaitouxiangAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    //
//    self.user_name = [UIButton buttonWithType:(UIButtonTypeSystem)];
//    self.user_name.frame = CGRectMake(50, CGRectGetMaxY(self.image_touxiang.frame) + 15, SCREEN_WIDTH - 100, 20);
//    [self.user_name setTitle:@"1526999999999" forState:(UIControlStateNormal)];
//    [self.user_name setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//    self.user_name.titleLabel.textAlignment = NSTextAlignmentCenter;
//    self.user_name.titleLabel.font = [UIFont systemFontOfSize:15];
//    [self.view_headView addSubview:self.user_name];
//    [self.user_name addTarget:self action:@selector(btn_xiugaitouxiangAction:) forControlEvents:(UIControlEventTouchUpInside)];

    
    //下面固定的
    CGFloat length_x = (SCREEN_WIDTH - 1) / 2;
    
    UIView * view_line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(image_bg.frame) + 60, SCREEN_WIDTH, 1)];
    view_line.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view_headView addSubview:view_line];
    
    UIView * view_line1 = [[UIView alloc] initWithFrame:CGRectMake(length_x,CGRectGetMaxY(image_bg.frame) + 10, 1, 40)];
    view_line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view_headView addSubview:view_line1];
    
    //积分
    UIImageView * image_jifen = [[UIImageView alloc] initWithFrame:CGRectMake(23 ,CGRectGetMaxY(image_bg.frame) + 20, 20, 20)];
    image_jifen.image = [UIImage imageNamed:@"jifen@2x.png"];
    [self.view_headView addSubview:image_jifen];
    
    self.jifen = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_jifen.frame) + 10, CGRectGetMaxY(image_bg.frame) + 20, length_x - CGRectGetMaxX(image_jifen.frame) - 15, 20)];
    self.jifen.text = @"积分:100000";
    self.jifen.font = [UIFont systemFontOfSize:16];
    [self.view_headView addSubview:self.jifen];
    
    UIButton * btn_jifen = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_jifen.frame = CGRectMake(0, CGRectGetMaxY(image_bg.frame), length_x, 60);
    [self.view_headView addSubview:btn_jifen];
    [btn_jifen addTarget:self action:@selector(btn_jifenAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //钱包
    UIImageView * image_qianbao = [[UIImageView alloc] initWithFrame:CGRectMake(length_x + 24 ,CGRectGetMaxY(image_bg.frame) + 20, 20, 20)];
    image_qianbao.image = [UIImage imageNamed:@"qianbao@2x.png"];
    [self.view_headView addSubview:image_qianbao];
    
    self.qianbao = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_qianbao.frame) + 10, CGRectGetMaxY(image_bg.frame) + 20, length_x * 2 - CGRectGetMaxX(image_qianbao.frame) - 15, 20)];
    self.qianbao.text = @"钱包:100000";
    self.qianbao.font = [UIFont systemFontOfSize:16];
    [self.view_headView addSubview:self.qianbao];
    
    UIButton * btn_qianbao = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_qianbao.frame = CGRectMake(length_x + 1, CGRectGetMaxY(image_bg.frame), length_x, 60);
    [self.view_headView addSubview:btn_qianbao];
    [btn_qianbao addTarget:self action:@selector(btn_qianbaoAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //5个状态
    CGFloat length = SCREEN_WIDTH / 5;
    //1
    UIImageView * image_1 = [[UIImageView alloc] initWithFrame:CGRectMake(length / 2 - 10, CGRectGetMaxY(view_line.frame) + 15, 20, 20)];
    image_1.image = [UIImage imageNamed:@"daizhifu@2x.png"];
    [self.view_headView addSubview:image_1];
    
    UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(3, CGRectGetMaxY(image_1.frame) + 10, length - 6, 15)];
    label_1.text = @"待支付";
    label_1.textAlignment = NSTextAlignmentCenter;
    label_1.font = [UIFont systemFontOfSize:14];
    [self.view_headView addSubview:label_1];
    
    UIButton * btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_1.frame = CGRectMake(3, CGRectGetMaxY(view_line.frame) , length - 6, 65);
    btn_1.tag = 10;
    [self.view_headView addSubview:btn_1];
    [btn_1 addTarget:self action:@selector(btn_typeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //2
    UIImageView * image_2 = [[UIImageView alloc] initWithFrame:CGRectMake(length +  length / 2 - 10, CGRectGetMaxY(view_line.frame) + 15, 20, 20)];
    image_2.image = [UIImage imageNamed:@"daijiedan@2x.png"];
    [self.view_headView addSubview:image_2];
    
    UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake(length + 3, CGRectGetMaxY(image_2.frame) + 10, length - 6, 15)];
    label_2.text = @"待接单";
    label_2.textAlignment = NSTextAlignmentCenter;
    label_2.font = [UIFont systemFontOfSize:14];
    [self.view_headView addSubview:label_2];
    
    UIButton * btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_2.frame = CGRectMake(3 + length, CGRectGetMaxY(view_line.frame) , length - 6, 65);
    btn_2.tag = 11;
    [self.view_headView addSubview:btn_2];
    [btn_2 addTarget:self action:@selector(btn_typeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //3
    UIImageView * image_3 = [[UIImageView alloc] initWithFrame:CGRectMake(length * 2 +  length / 2 - 10, CGRectGetMaxY(view_line.frame) + 15, 20, 20)];
    image_3.image = [UIImage imageNamed:@"peisong@2x.png"];
    [self.view_headView addSubview:image_3];
    
    UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(length * 2 + 3, CGRectGetMaxY(image_3.frame) + 10, length - 6, 15)];
    label_3.text = @"配送中";
    label_3.textAlignment = NSTextAlignmentCenter;
    label_3.font = [UIFont systemFontOfSize:14];
    [self.view_headView addSubview:label_3];
    
    UIButton * btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_3.frame = CGRectMake(3 + length * 2, CGRectGetMaxY(view_line.frame) , length - 6, 65);
    btn_3.tag = 12;
    [self.view_headView addSubview:btn_3];
    [btn_3 addTarget:self action:@selector(btn_typeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //4
    UIImageView * image_4 = [[UIImageView alloc] initWithFrame:CGRectMake(length * 3 +  length / 2 - 10, CGRectGetMaxY(view_line.frame) + 15, 20, 20)];
    image_4.image = [UIImage imageNamed:@"yiwancheng@2x.png"];
    [self.view_headView addSubview:image_4];
    
    UILabel * label_4 = [[UILabel alloc] initWithFrame:CGRectMake(length * 3 + 3, CGRectGetMaxY(image_4.frame) + 10, length - 6, 15)];
    label_4.text = @"已完成";
    label_4.textAlignment = NSTextAlignmentCenter;
    label_4.font = [UIFont systemFontOfSize:14];
    [self.view_headView addSubview:label_4];
    
    UIButton * btn_4 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_4.frame = CGRectMake(3 + length * 3, CGRectGetMaxY(view_line.frame) , length - 6, 65);
    btn_4.tag = 13;
    [self.view_headView addSubview:btn_4];
    [btn_4 addTarget:self action:@selector(btn_typeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //5
    UIImageView * image_5 = [[UIImageView alloc] initWithFrame:CGRectMake(length * 4 + length / 2 - 10, CGRectGetMaxY(view_line.frame) + 15, 20, 20)];
    image_5.image = [UIImage imageNamed:@"quexiaodingdan @2x.png"];
    [self.view_headView addSubview:image_5];
    
    UILabel * label_5 = [[UILabel alloc] initWithFrame:CGRectMake(length * 4 + 3, CGRectGetMaxY(image_5.frame) + 10, length - 6, 15)];
    label_5.text = @"已取消";
    label_5.textAlignment = NSTextAlignmentCenter;
    label_5.font = [UIFont systemFontOfSize:14];
    [self.view_headView addSubview:label_5];
    
    UIButton * btn_5 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_5.frame = CGRectMake(3 + length * 4, CGRectGetMaxY(view_line.frame) , length - 6, 65);
    btn_5.tag = 14;
    [self.view_headView addSubview:btn_5];
    [btn_5 addTarget:self action:@selector(btn_typeAction:) forControlEvents:(UIControlEventTouchUpInside)];
}







@end
